package com.zhuyiyi.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuyiyi.mmall.entity.User;
import com.zhuyiyi.mmall.entity.UserAddress;
import com.zhuyiyi.mmall.mapper.UserAddressMapper;
import com.zhuyiyi.mmall.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 11
 * @since 2021-05-10
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    @Autowired
    private UserAddressServiceImpl userAddressService;

    @Override
    public boolean updateById(String id, String address, String remark, String code, HttpSession session) {
        User user=(User)session.getAttribute("user");
        int isdefault;
        if (code.equals("是") ){
            isdefault=1;
        }
        else {
            isdefault=0;
        }
        Integer uid = Integer.parseInt(id);

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("id", uid);
        UserAddress userAddress = userAddressService.getOne(wrapper);
        userAddress.setAddress(address);
        userAddress.setRemark(remark);
        if (userAddress.getIsdefault()== 1) {
            if (isdefault == 0)
                return false;
        } else {
            if (isdefault == 1) {
                QueryWrapper wrapper1 = new QueryWrapper();
                wrapper1.eq("isdefault", isdefault);
                wrapper1.eq("user_id",user.getId());
                UserAddress oldDefault = userAddressService.getOne(wrapper1);
                oldDefault.setIsdefault(0);
                userAddressService.updateById(oldDefault);
                userAddress.setIsdefault(1);
            }
        }


        userAddressService.updateById(userAddress);
        return false;

    }


}
